package com.syqc.maps.utils

import android.os.Bundle
import android.os.PersistableBundle
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.MapsInitializer
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.MarkerOptions
import com.syqc.comlib.base.BaseActivity
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityDialogLocationBinding

class LocationDialogActivity :
    BaseActivity<ActivityDialogLocationBinding>(ActivityDialogLocationBinding::inflate) {
    private lateinit var mAMap: AMap
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MapsInitializer.updatePrivacyShow(this, true, true)
        MapsInitializer.updatePrivacyAgree(this, true)
        binding.mMapView.onCreate(savedInstanceState)
        mAMap = binding.mMapView.map
        initMap()
    }

    override fun onResume() {
        super.onResume()
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        binding.mMapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        binding.mMapView.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        binding.mMapView.onSaveInstanceState(outState)
    }

    private fun initMap() {
        initMapSetting()
        if (latlng != null) {
            mAMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, 18f))
            mAMap.addMarker(
                MarkerOptions().position(latlng)
                    .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_location))
            )
        }
    }

    private var latlng: LatLng? = null;
    override fun initView() {
        intent?.run {
            latlng = LatLng(getDoubleExtra("lat", 0.0), getDoubleExtra("lng", 0.0))
        }

        binding.flMapLocation.setOnClickListener {
            finish()
        }
    }

    override fun setListener() {

    }

    private fun initMapSetting() {

        mAMap.uiSettings.run {
            isZoomControlsEnabled = false
            isMyLocationButtonEnabled = false
            isScaleControlsEnabled = true
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        latlng = null
        mAMap.clear()
        binding.mMapView.onDestroy()
    }


}